package interview.juc.task;

import io.netty.util.concurrent.DefaultThreadFactory;
import lombok.val;

import java.util.concurrent.*;

/**
 * @Author: zhangrongqing
 * @Date: 2019/12/30 11:18
 */
public class FutureTaskMain {
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(5,5,0, TimeUnit.SECONDS
                ,new ArrayBlockingQueue<>(10),new DefaultThreadFactory("test"));
        Long startTime = System.currentTimeMillis();
        //直接使用excutorservice的submit方法
//        try {
//            String str = executorService.submit(() -> new FutureService1().call()).get();
//            String str2 = executorService.submit(() -> new FutureService2().call()).get();
//            System.out.println(str);
//            System.out.println(str2);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }finally {
//            executorService.shutdown();
//        }
        CompletableFuture<String> str = CompletableFuture.supplyAsync(() -> new FutureService1().call(),executorService);
        CompletableFuture<String> str2 = CompletableFuture.supplyAsync(() -> new FutureService2().call(),executorService);

        str.thenAccept((e) -> System.out.println(e)).join();
        str2.thenAccept(e -> System.out.println(e)).join();
//        System.out.println(str.get());
//        System.out.println(str2.get());
        System.out.println("程序执行时间：" + (System.currentTimeMillis() - startTime));
        executorService.shutdown();
    }
}
